package com.dcms.power.controller;

import com.dcms.power.annotation.ArchivesLog;
import com.dcms.admin.controller.BaseController;
import com.dcms.power.bean.UserRole;
import com.dcms.power.service.IUserRoleService;
import com.dcms.util.layui.LayTable;
import com.dcms.util.ResultBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * \*
 * \* @name: RoleController
 * \* @author: 武佳伟丶
 * \* @date: 2018/4/8 0008
 * \* @time: 19:23
 * \* @description: To change this template use File | Settings | File Templates.
 * \
 */
@Controller
@RequestMapping("power/userRole")
public class UserRoleController extends BaseController {

    @Autowired
    private IUserRoleService userRoleService;

    @RequestMapping("query")
    @ResponseBody
    public LayTable<UserRole> query(
            String param,
            UserRole record,
            Integer limit,
            Integer page
    ){
        page = page == null ? 1 : page;
        limit = limit == null ? 12 : limit;

        PageHelper.startPage(page, limit);
        List<UserRole> list = userRoleService.selectAll(param, record);
        PageInfo<UserRole> info = new PageInfo<UserRole>(list);
        LayTable<UserRole> bean = new LayTable<UserRole>(info.getList(), (int)info.getTotal());
        return bean;
    }

    /**
     * @author: 武佳伟丶
     * @description: 删除，支持批量
     * @date: 14:39 2018/2/12 0012
     * @param: [id]
     * @return: com.dcms.util.ResultBean
     */
    @ArchivesLog(message = "删除用户角色关联")
    @RequestMapping("delete")
    @ResponseBody
    public ResultBean deleteBatch(UserRole record){
        int rows = userRoleService.deleteSelective(record);
        if (rows > 0){
            return new ResultBean();
        }
        return new ResultBean(10021);
    }

    /**
     * @author: 武佳伟丶
     * @description: 更新
     * @date: 18:55 2018/2/13 0013
     * @param: [record]
     * @return: com.dcms.util.ResultBean
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    @ArchivesLog(message = "更新用户角色信息")
    public ResultBean update(UserRole record){
        record.setUpdateTime(new Date());
        int rows = userRoleService.updateByPrimaryKeySelective(record);
        if (rows > 0){
            return new ResultBean();
        }
        return new ResultBean(10021);
    }

    /**
     * @author: 武佳伟丶
     * @description: 跳转到数据编辑页，用于新增数据
     * @date: 14:42 2018/2/18 0018
     * @param: [userRoleId, request]
     * @return: java.lang.String
     */
    @RequestMapping("toinsert")
    public String toInsert(){
        return "admin/userRole/edit";
    }

    /**
     * @author: 武佳伟丶
     * @description: 录入数据
     * @date: 14:52 2018/2/18 0018
     * @param: [record]
     * @return: com.dcms.util.ResultBean
     */
    @RequestMapping("insert")
    @ResponseBody
    @ArchivesLog(message = "录入新的用户角色")
    public ResultBean insert(UserRole record){
        Date now = new Date();
        record.setCtime(now);
        record.setUpdateTime(now);
        record.setCreator(getCurrentUser().getUserId());
        int rows = userRoleService.insertSelective(record);
        if (rows > 0){
            return  new ResultBean();
        }
        return new ResultBean();
    }
    
    /**
     * @author: 武佳伟丶 
     * @description: 根据userid获取role列表
     * @date: 14:38 2018/4/10 0010
     * @param: [userId]
     * @return: com.dcms.util.ResultBean
     */
    @RequestMapping("getRoleByUserId")
    @ResponseBody
    public ResultBean getRoleByUserId(Integer userId){
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        List<UserRole> list = userRoleService.selectAll(null, userRole);
        if (list.size() > 0){
            return new ResultBean(200, "查询成功", list);
        }
        return new ResultBean(ResultBean.Code.fail);
    }
}